<template>
    <div class="e-form">
        <div class="tabs" v-if="Object.keys(formData).length != 0">
            <el-tabs tab-position="left" v-model="tabsName" @tab-click="onChangeTab">
                <!-- 基本信息 -->
                <el-tab-pane
                    label="基本信息"
                    name="baseInfo"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane label="包件信息" name="detail" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane
                    label="采购清单"
                    name="tenderList"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane label="公文文件" name="files" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane label="采购条款" name="clause" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane label="附件信息" name="attach" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane
                    label="审核历史"
                    name="auditHistory"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane label="补遗记录" name="recodes" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane label="招标报名" name="reg" :disabled="clickTabFlag"></el-tab-pane>
                <el-tab-pane
                    label="评标结果登记"
                    name="bidEvaluation"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane
                    label="招标结果公示"
                    name="biddingPublicity"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane
                    label="中标通知书"
                    name="bidWinningNotice"
                    :disabled="clickTabFlag"
                ></el-tab-pane>
                <el-tab-pane label="关闭记录" name="close" :disabled="clickTabFlag"></el-tab-pane>
                <div>
                    <div id="baseInfoCon" class="con">
                        <div class="tabs-title" id="baseInfo">基本信息</div>
                        <div style="width: 100%" class="form">
                            <el-form ref="form" :model="form" label-width="170px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="采购编号" width="220">
                                            <span>{{ formData.billNo }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="采购人" width="230">
                                            <span>{{ formData.tenderUser }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <!-- 需要id和name -->
                                        <el-form-item label="申请机构" width="230">
                                            <span>{{ formData.applyOrgName }}</span>
                                            <i
                                                class="el-icon-document-copy"
                                                slot="suffix"
                                                @click="stateChange('selectData')"
                                            >
                                            </i>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="申请日期">
                                            <span>{{ formData.applyTime }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="采购方式">
                                            <span>{{ formData.tenderForm }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="招标类型：">
                                            <span>{{
                                                formData.tenderClass | tenderClass(this)
                                            }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="拟采购项目名称" width="200">
                                            <span>{{ formData.tenderName }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="拟采购金额（元）">
                                            <span>{{ formData.tenderAmount }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="集中采购">
                                            <el-checkbox
                                                v-model="formData.isCentralize"
                                                :true-label="1"
                                                :false-label="0"
                                                disabled
                                            ></el-checkbox>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="税额">
                                            <span>{{ formData.taxAmount }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <!-- <el-col :span="12">
                                        <el-form-item label="录入机构">
                                            <span>{{ formData.orgName }}</span>
                                        </el-form-item>
                                    </el-col> -->
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="投标保证金（元）">
                                            <span>{{ formData.tenderBail }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <!-- 返回id、名字 -->
                                        <el-form-item label="币种">
                                            <span>{{ formData.currency }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="购买标书费用（元）">
                                            <span>{{ formData.bidCharge }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="采购公告">
                                            <el-button
                                                style="width: 222px"
                                                class="btn-greenYellow"
                                                >{{ formData.tenderNotice }}</el-button
                                            >
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="投标截止时间">
                                            <span>{{ formData.tenderEndTime }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="录入机构">
                                            <span>{{ formData.orgName }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="发布到招采平台">
                                            <span>{{
                                                formData.isTenderPlat === 0 ? '否' : '是'
                                            }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="状态">
                                            <span>{{ formData.state | state(this) }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-form-item label="采购需求陈述">
                                        <span>{{ formData.tenderDemand }}</span>
                                    </el-form-item>
                                </el-row>
                                <el-row>
                                    <el-form-item label="备注">
                                        <span>{{ formData.remarks }}</span>
                                    </el-form-item>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="本位币">
                                            <!-- 传id和名称 -->
                                            <span>{{ formData.baseCurName }}</span>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12" v-if="formData.baseCurName !== formData.currency">
                                        <el-form-item label="人民币汇率">
                                            <span>{{ formData.rmbRate }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="拟采购金额（本位币）">
                                            <span>{{ formData.tenderAmount }}</span>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                        </div>
                    </div>

                    <div id="detailCon" class="con">
                        <div class="tabs-title" id="detail">拟招标内容明细</div>
                        <div class="e-table">
                            <div class="top">
                                <div class="left">
                                    <div>
                                        <el-button size="small" class="btn-greenYellow"
                                            >新增</el-button
                                        >
                                    </div>
                                </div>
                            </div>
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <el-table-column show-overflow-tooltip prop="a1" label="包件编号">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a" label="包件名称">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="b" label="起止桩号">
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="c"
                                    label="招标最高限价(元)"
                                >
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="d" label="分包商名称">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="e" label="税率">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="f" label="税额">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip label="操作">
                                    <template slot-scope="scope">
                                        <span v-if="scope.row.g == '0'" style="color: #e85442"
                                            >未审核</span
                                        >
                                        <span v-if="scope.row.g == '1'" style="color: #4773db"
                                            >审核中</span
                                        >
                                        <span v-if="scope.row.g == '2'" style="color: #979797"
                                            >已审核</span
                                        >
                                    </template>
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                        </div>
                    </div>

                    <div id="tenderListCon" class="con">
                        <div class="tabs-title" id="tenderList">招标清单</div>
                        <div style="display: flex">
                            <div class="left">
                                <ComTreeFile />
                            </div>
                            <div class="right" style="margin-left: 20px; width: 100%">
                                <ComTreeTable />
                            </div>
                        </div>

                        <div class="tabs-title" id="files">公文文件</div>
                        <div class="e-table">
                            <div class="top">
                                <div class="left">
                                    <el-button size="mini">下载</el-button>
                                    <el-button size="mini">删除</el-button>
                                    <el-button
                                        size="mini"
                                        style="padding: 7px 30px"
                                        class="btn-greenYellow"
                                        >选择公文</el-button
                                    >
                                </div>
                            </div>
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <el-table-column show-overflow-tooltip width="55">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a1" label="序号">
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="a"
                                    label="公文文件名称"
                                >
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="b" label="上传时间">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="c" label="上传人">
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                        </div>
                    </div>

                    <div id="clauseCon" class="con">
                        <div class="tabs-title" id="clause">通用合同条款</div>
                        <div class="e-table" style="min-height: auto">
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <el-table-column show-overflow-tooltip prop="a1" label="序号">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a" label="所属单位">
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="b"
                                    label="通用合同条款编号"
                                >
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="c"
                                    label="合同条款名称"
                                >
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="d"
                                    label="条款明细查阅"
                                >
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                        </div>

                        <div class="tabs-title" style="margin-top: 20px">项目专用合同条款</div>
                        <div class="e-table" style="min-height: auto; margin-bottom: 20px">
                            <div class="top">
                                <div class="left">
                                    <div>
                                        <el-button size="small" class="btn-greenYellow"
                                            >新增</el-button
                                        >
                                        <el-button size="small" class="btn-greenYellow"
                                            >删除</el-button
                                        >
                                    </div>
                                </div>
                            </div>
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <div class="top">
                                    <div class="left">
                                        <el-button size="mini">新增</el-button>
                                        <el-button size="mini">删除</el-button>
                                    </div>
                                </div>
                                <el-table-column show-overflow-tooltip prop="a1" label="序号">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a" label="条目号">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="b" label="原条款内容">
                                </el-table-column>
                                <el-table-column
                                    show-overflow-tooltip
                                    prop="c"
                                    label="专用合同条款内容"
                                >
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                        </div>
                    </div>

                    <div id="attachCon" class="con">
                        <div class="tabs-title" id="attach">附件信息</div>
                        <div class="e-table">
                            <div class="top">
                                <div class="left">
                                    <el-button size="mini">下载</el-button>
                                    <el-button size="mini">删除</el-button>
                                </div>
                            </div>
                            <el-table
                                :data="tableData"
                                border
                                style="width: 100%"
                                @cell-mouse-enter="cellMouseEnter"
                                @cell-mouse-leave="cellMouseLeave"
                                :cell-class-name="cellClsNm"
                            >
                                <el-table-column show-overflow-tooltip width="55">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a1" label="序号">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="a" label="文件名称">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="b" label="文件大小">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="c" label="上传时间">
                                </el-table-column>
                                <el-table-column show-overflow-tooltip prop="d" label="上传人">
                                </el-table-column>
                            </el-table>
                            <ComPagination />
                            <div class="upload">
                                <el-upload
                                    class="upload-demo"
                                    drag
                                    action="https://jsonplaceholder.typicode.com/posts/"
                                    multiple
                                >
                                    <el-button type="primary" style="padding: 20px 80px"
                                        >上传附件</el-button
                                    >
                                    <div style="margin-top: 10px; font-size: 12px">
                                        鼠标拖入上传，按住Ctrl可多选附件
                                    </div>
                                </el-upload>
                            </div>
                        </div>
                    </div>

                    <div id="auditHistoryCon" class="con">
                        <div class="tabs-title" id="auditHistory">审核历史</div>
                        <el-table
                            :data="tableData"
                            border
                            style="width: 100%"
                            @cell-mouse-enter="cellMouseEnter"
                            @cell-mouse-leave="cellMouseLeave"
                            :cell-class-name="cellClsNm"
                        >
                            <el-table-column show-overflow-tooltip prop="a1" label="审核级次">
                            </el-table-column>
                            <el-table-column show-overflow-tooltip prop="a" label="审核人">
                            </el-table-column>
                            <el-table-column show-overflow-tooltip prop="b" label="审核人岗位">
                            </el-table-column>
                            <el-table-column show-overflow-tooltip prop="c" label="通知时间">
                            </el-table-column>
                            <el-table-column show-overflow-tooltip prop="c" label="审核时间">
                            </el-table-column>
                            <el-table-column show-overflow-tooltip prop="c" label="审核意见">
                            </el-table-column>
                        </el-table>
                        <ComPagination />
                    </div>

                    <div id="recodesCon" class="con">
                        <div class="tabs-title" id="recodes">补遗记录</div>
                        <ComTenderAddendumIndex v-if="ifLoadComponent('recodes')" />
                        <div class="seize-a-seat" v-else>
                            <i class="el-icon-loading"></i>
                        </div>
                    </div>

                    <div id="regCon" class="con">
                        <div class="tabs-title" id="reg">报名记录</div>
                        <ComOpenBiddingRegIndex v-if="ifLoadComponent('reg')" />
                        <div class="seize-a-seat" v-else>
                            <i class="el-icon-loading"></i>
                        </div>
                    </div>

                    <div id="bidEvaluationCon" class="con">
                        <div class="tabs-title" id="bidEvaluation">评标结果记录</div>
                        <ComBidEvaluationResultIndex v-if="ifLoadComponent('bidEvaluation')" />
                        <div class="seize-a-seat" v-else>
                            <i class="el-icon-loading"></i>
                        </div>
                    </div>

                    <div id="biddingPublicityCon" class="con">
                        <div class="tabs-title" id="biddingPublicity">招标公示记录</div>
                        <ComBiddingResultIndex v-if="ifLoadComponent('biddingPublicity')" />
                        <div class="seize-a-seat" v-else>
                            <i class="el-icon-loading"></i>
                        </div>
                    </div>

                    <div id="bidWinningNoticeCon" class="con">
                        <div class="tabs-title" id="bidWinningNotice">中标通知书记录</div>
                        <ComBidWinningNoticeIndex v-if="ifLoadComponent('bidWinningNotice')" />
                        <div class="seize-a-seat" v-else>
                            <i class="el-icon-loading"></i>
                        </div>
                    </div>

                    <div id="closeCon" class="con">
                        <div class="tabs-title" id="close">关闭记录</div>
                        <ComBidClosingIndex v-if="ifLoadComponent('close')" />
                        <div class="seize-a-seat" v-else>
                            <i class="el-icon-loading"></i>
                        </div>
                    </div>
                </div>
            </el-tabs>
        </div>
        <div class="buttons">
            <el-button @click="handleClose">取消</el-button>
        </div>
        <div id="seat" :style="{ height: seatHeight + 'px' }"></div>
    </div>
</template>
<script>
import '@/utils/jquery.scrollTo.min'
import applyRequest from '@/api/tender/apply.js'
import ComTreeTable from '@/templates/table/treeTable.vue'
import ComTreeFile from '@/templates/table/treeFile.vue'
import ComPagination from '@/components/pagination/pagination.vue'
import ComTenderAddendumIndex from '@/pages/bidding/equipmentBidding/tenderAddendum/tenderAddendumIndex'
import ComBidClosingIndex from '@/pages/bidding/equipmentBidding/bidClosing/bidClosingIndex'
import ComBiddingResultIndex from '@/pages/bidding/equipmentBidding/biddingResult/biddingResultIndex'
import ComBidEvaluationResultIndex from '@/pages/bidding/equipmentBidding/bidEvaluationResult/bidEvaluationResultIndex'
import ComBidWinningNoticeIndex from '@/pages/bidding/equipmentBidding/bidWinningNotice/bidWinningNoticeIndex'
import ComOpenBiddingRegIndex from '@/pages/bidding/equipmentBidding/openBiddingReg/openBiddingRegIndex'
import { mapState } from 'vuex'

export default {
    data () {
        return {
            tabsName: 'baseInfo',
            screenWidth: 0,
            screenHeight: 0,
            lastConHeight: 0,
            clickTabFlag: false, // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: ''
            },
            tableData: [],
            value: '',
            textarea: '',
            formData: {},
            currentTabComponentName: '',
            loadComponent: new Set()
        }
    },
    computed: {
        // 填补底部空白，以使高度够滚动
        seatHeight () {
            return this.screenHeight - 72 - this.lastConHeight
        },
        ...mapState({
            options: state => state.biddingApplication.dictionaries
        }),
        tenderId () {
            return this.$route.query.tenderId || this.$route.query.billid
        }
    },
    components: {
        ComTreeTable,
        ComTreeFile,
        ComPagination,
        ComTenderAddendumIndex,
        ComBidClosingIndex,
        ComBiddingResultIndex,
        ComBidEvaluationResultIndex,
        ComBidWinningNoticeIndex,
        ComOpenBiddingRegIndex
    },
    created () {
        this.getData()
    },
    mounted () {
        // 保存所有tabName
        const arr = [
            'baseInfo',
            'detail',
            'tenderList',
            'files',
            'clause',
            'attach',
            'auditHistory',
            'recodes',
            'close',
            'reg',
            'bidEvaluation',
            'biddingPublicity',
            'bidWinningNotice'
        ]
        // 获取最后一个内容区域的高度，计算底部空白
        this.getLastConHeight(arr)
        let $idsTop = []
        window.addEventListener('scroll', () => {
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            if (this.clickTabFlag) {
                return
            }

            if (!$idsTop[$idsTop.length - 1]) {
                $idsTop = arr.map(item => {
                    const itemTop = document.getElementById(item)?.offsetTop
                    return itemTop
                })
            }
            const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
            // 倒序查找
            let curLocal = 0
            for (let len = $idsTop.length, i = len - 1; i >= 0; i--) {
                let item = $idsTop[i]
                if (scrollTop + 1 >= item) {
                    curLocal = i
                    break
                }
            }
            // 设置对应tabName
            this.tabsName = arr[curLocal]
            // 按需加载组件
            this.loadComponent.add(this.tabsName)
        })

        this.screenWidth = document.documentElement.clientWidth - this.topHeight
        this.screenHeight = document.documentElement.clientHeight - this.topHeight
        window.addEventListener('resize', () => {
            this.screenWidth = document.documentElement.clientWidth - this.topHeight
            this.screenHeight = document.documentElement.clientHeight - this.topHeight
            $idsTop = arr.map(item => {
                const itemTop = document.getElementById(item)?.offsetTop
                return itemTop
            })
        })

        // 按需加载组件
        this.loadComponent.add(this.tabsName)
    },
    methods: {
        ifLoadComponent (component) {
            return this.loadComponent.has(component)
        },
        currentTabComponent (name) {
            return this.currentTabComponentName === name
        },
        showBidding () {},
        // 获取最后一个内容区域的高度，计算底部空白
        getLastConHeight (arr) {
            let si = setInterval(() => {
                // 因为dom异步加载，通过轮询找到渲染后的dom，获取高度
                if (document.getElementById(arr[length - 1] + 'Con')) {
                    const lastConHeight = document.getElementById(
                        arr[length - 1] + 'Con'
                    )?.offsetHeight
                    this.lastConHeight = lastConHeight
                    clearInterval(si)
                    si = null
                }
            }, 100)
        },
        onChangeTab (e) {
            $.scrollTo(`#${e.name}`, 500)
            // 如果手动点击时，则不做滚动事件逻辑，只有滚动屏幕时才走滚动事件逻辑
            this.clickTabFlag = true
            // 动画结束后，恢复状态
            setTimeout(() => {
                this.clickTabFlag = false
            }, 600)
            // 按需加载组件
            this.loadComponent.add(e.name)
        },
        getData () {
            applyRequest.tenderApplyFindById(this.tenderId).then(res => {
                this.formData = res
                // res
            })
        },
        handleFullScreen () {
            this.fullscreen = !this.fullscreen
        },
        handleClose () {
            // this.dialogVisible = false
            this.$router.push('./equipmentBiddingApplication')
        },
        cellClsNm ({ column }) {
            if (column.label === '') {
                return 'fixed-left'
            }
        },
        cellMouseEnter (row) {
            this.MouseEnterId = row.id
        },
        cellMouseLeave () {
            this.MouseEnterId = 0
        }
    }
}
</script>

<style lang="scss" scoped>
.form {
    margin-top: 10px;
}

.e-table {
    background: #fff;

    /* .top {
    .left {
      padding: 20px;
      padding-top: 0px;
    }
  } */
}

.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
